Potential fix for code scanning alert no. 3: DOM text reinterpreted as HTML - #153
Merged
Conversation
…s HTML Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
poad
marked this pull request as ready for review
January 1, 2026 09:33
poad
enabled auto-merge (squash)
January 1, 2026 09:34
Contributor
There was a problem hiding this comment.
セキュリティ修正のレビュー結果
このPRは、DOM text reinterpretation脆弱性(Code Scanning Alert #3)に対する重要なセキュリティ修正を実装しています。
主な改善点
sanitizeUrl関数の追加により、iframe srcでの危険なプロトコル(javascript:など)の実行を防止- URL検証により、HTTPSとHTTPのみを許可する適切な制限を実装
修正が必要な問題
- サーバーサイドレンダリング対応:
window.location.originへの依存を削除し、絶対URLのみを受け入れるように修正 - プロトコル検証の強化: より厳密な否定的検証ロジックの実装
- 入力検証の一貫性: URL入力フィールドでも同様の検証を適用することを推奨
セキュリティ修正の方向性は正しいですが、上記の改善により、より堅牢な実装になります。
You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.
| const sanitizeUrl = (value: string | undefined | null): string | undefined => { | ||
| if (!value) return undefined; | ||
| try { | ||
| const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined); |
Contributor
There was a problem hiding this comment.
🛑 セキュリティ脆弱性: sanitizeUrl関数でサーバーサイドレンダリング時にベースURLが未定義になる可能性があります。window.location.originが利用できない場合、相対URLが予期しない動作を引き起こす可能性があります1。
Suggested change
| const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined); | |
| const url = new URL(value); |
Footnotes
-
CWE-20: Improper Input Validation - https://cwe.mitre.org/data/definitions/20.html ↩
Comment on lines
+25
to
+27
| if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
| return url.toString(); | ||
| } |
Contributor
There was a problem hiding this comment.
プロトコル検証が不完全です。`
Suggested change
| if (url.protocol === 'http:' || url.protocol === 'https:') { | |
| return url.toString(); | |
| } | |
| if (url.protocol !== 'http:' && url.protocol !== 'https:') { | |
| return undefined; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Potential fix for https://github.com/poad/llm-ts-example/security/code-scanning/3
In general terms, the problem should be fixed by validating and constraining the user-provided string before using it as the
srcof the<iframe>. The validation should ensure that only well-formed URLs with allowed schemes (e.g.,http:andhttps:) are accepted, and anything else is rejected or results in no navigation. This avoids interpreting arbitrary DOM text as a URL/HTML-like context and blocks vectors such asjavascript:alert(1)or malformed URLs.For this specific code, the least invasive fix is:
URLobject and confirms that the protocol is one of an allowlist, likehttp:orhttps:. If parsing fails or the protocol is not allowed, returnundefined.src: instead of(src ?? url) || undefined, compute asafeSrcas either the sanitizedsrcprop or the sanitized contexturl, and pass that tosrc. If both are invalid, the iframe will havesrc={undefined}, effectively loading nothing.These changes all live in
chat-app/components/ai-elements/web-preview.tsx. We will:sanitizeUrlhelper function near the top of the file (where it’s easy to see and reuse).WebPreviewBody, computeconst safeSrc = sanitizeUrl(src ?? url);and usesafeSrc || undefinedfor the iframesrc.No external libraries are strictly required; we can safely use the built-in
URLclass in modern browsers/Next.js.Suggested fixes powered by Copilot Autofix. Review carefully before merging.